package com.atguigu.gmall.common.config.minio;

import com.atguigu.gmall.common.config.minio.service.FileUploadService;
import com.atguigu.gmall.common.config.minio.service.impl.FileUploadServiceImpl;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author:ahang
 * @create:2022-09-28 23:26
 * @Description:
 */
//1.MinioProperties所有属性和文件进行绑定 2.MinioProperties自动放进容器中
@EnableConfigurationProperties(MinioProperties.class)
@Configuration
public class MinioAutoConfiguration {

    @Autowired
    MinioProperties minioProperties;

    @Bean
    public MinioClient minioClient() throws Exception {
        //创建minio对象,放在spring容器中,避免多次创建对象
        MinioClient minioClient = new MinioClient(minioProperties.getEndpoint(),
                minioProperties.getAccessKey(),
                minioProperties.getSecretKey());
        //判断桶是否存在
        boolean isExist = minioClient.bucketExists(minioProperties.getBucketName());
        if(!isExist){
            minioClient.makeBucket(minioProperties.getBucketName());
        }
        //返回对象
        return minioClient;
    }

    @Bean
    public FileUploadService fileUploadService(){
        FileUploadServiceImpl fileUploadServiceImpl = new FileUploadServiceImpl();
        return fileUploadServiceImpl;
    }
}
